package com.sise.studentInfoSystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sise.studentInfoSystem.domain.*;
import com.sise.studentInfoSystem.mapper.*;
import com.sise.studentInfoSystem.template.OssTemplate;
import com.sise.studentInfoSystem.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zzp
 * @Desc
 * @Date 2020/7/4
 */
@Controller
public class SurveyTeamController {

    @Autowired
    OssTemplate ossTemplate;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    AcademicMapper academicMapper;

    @Autowired
    PrehensiveMapper prehensiveMapper;

    @Autowired
    ProfrssionalSkillsMapper profrssionalSkillsMapper;

    @Autowired
    ExtraPointsMapper extraPointsMapper;




    @RequestMapping("/toSurveyCreativeAbility")
    public String toCreativeAbility() {
        return "surveyTeam/outstands/stuSurvey_creativeAbility";
    }

    @RequestMapping("/toSurveyOrganizationManagement")
    public String toOrganizationManagement() {
        return "surveyTeam/outstands/stuSurvey_organizationManagement";
    }

    @RequestMapping("/toSurveyStylisticAbility")
    public String toStylisticAbility() {
        return "surveyTeam/outstands/stuSurvey_stylisticAbility";
    }

    @RequestMapping("/toSurveySocialPractice")
    public String toSocialPractice() {
        return "surveyTeam/outstands/stuSurvey_socialPractice";
    }






    /**
     * 跳转审核专业技能页面
     * @return
     */
    @RequestMapping("/toSurveyProfessionalAbility/{curPage}/{pageSize}")
    public String toSurveyProfessionalAbility(@PathVariable Integer curPage, @PathVariable Integer pageSize, String condition, HttpSession session, Model model) {

        // 分页查询，开始记录位置
        int begin = (curPage - 1) * pageSize;

        // 登入的测评小组成员信息
        TStudent student = (TStudent) session.getAttribute("loginSurvey");
        // 获取其所在的班级
        String classes = student.getClasses();
        // 初始化一个VoList
        List<ProfessionalVo> professionalVos = new ArrayList<>();
        // 查询其班级的所有同学
        List<TStudent> students = new ArrayList<>();
        int countStu = 0;
        if (null != condition) {
            students = studentMapper.findStuByClassessAndCondition(classes, condition, begin, pageSize);
            // 回显查询数据
            model.addAttribute("searchName", condition);
            if (students.size() >= 1) {
                countStu = students.size();
            } else {
                countStu = studentMapper.findStuCountByClasses(classes);
            }
        } else {
            students = studentMapper.findStuByClassess(classes, begin, pageSize);
            // 查询班级人数
            countStu = studentMapper.findStuCountByClasses(classes);
        }


        ProfrssionalSkills profrssionalSkill = null;
        for (TStudent tStudent : students) {
            // 拿到同班同学的id
            String stuId = tStudent.getStuId();
            List<ProfrssionalSkills> profrssionalSkills = profrssionalSkillsMapper.findProfrssionalById(stuId);
            // 判断该同学是否提交专业技能
            if (profrssionalSkills.isEmpty()) {
                // 该同学未提交专业技能
                profrssionalSkill = new ProfrssionalSkills();
                profrssionalSkill.setStuId(tStudent.getStuId());
                profrssionalSkill.setStatus(0);
            } else {
                profrssionalSkill = profrssionalSkills.get(0);
            }
            // new一个vo对象
            ProfessionalVo vo = new ProfessionalVo();
            vo.setStuName(tStudent.getStuName());
            vo.setStuId(tStudent.getStuId());
            vo.setStatus(profrssionalSkill.getStatus());
            // 将组装好的vo对象，加入集合
            professionalVos.add(vo);
        }

        // 计算总页数
        int totalPageNum = (countStu + pageSize - 1) / pageSize;


        // 将vo对象放入请求域
        model.addAttribute("professionals", professionalVos);
        // 总页数
        model.addAttribute("totalPageNum", totalPageNum);
        // 当前前页
        model.addAttribute("page", curPage);

        return "surveyTeam/outstands/stuSurvey_professionalAbility";
    }

    /**
     * 分页查询审核学业水平条目
     *
     * @param curPage
     * @param pageSize
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/findAcademicPage/{curPage}/{pageSize}")
    public String findPage(@PathVariable Integer curPage, @PathVariable Integer pageSize, String condition, HttpSession session, Model model) {

        int begin = (curPage - 1) * pageSize;

        // 查询集合
        TAcademic stuAcademic = null;
        // 登入的测评小组成员信息
        TStudent student = (TStudent) session.getAttribute("loginSurvey");
        // 获取其所在的班级
        String classes = student.getClasses();
        // 初始化一个VoList
        List<CheckAcademicVo> checkAcademicVos = new ArrayList<>();
        // 查询其班级的所有同学
        List<TStudent> students = null;
        int countStu = 0;
        if (null != condition) {
            students = studentMapper.findStuByClassessAndCondition(classes, condition, begin, pageSize);
            model.addAttribute("searchName", condition);
            if (students.size() >= 1) {
                countStu = students.size();
            } else {
                countStu = studentMapper.findStuCountByClasses(classes);
            }
        } else {
            students = studentMapper.findStuByClassess(classes, begin, pageSize);
            // 查询班级人数
            countStu = studentMapper.findStuCountByClasses(classes);
        }

        for (TStudent tStudent : students) {
            // 拿到同班同学的id
            String stuId = tStudent.getStuId();
            stuAcademic = studentMapper.findAcademicById(stuId);
            // 判断该同学是否提交
            if (stuAcademic == null) {
                // 该同学未提交学业表现
                stuAcademic = new TAcademic();
                stuAcademic.setBasicPoints(0L);
                stuAcademic.setFailedNum(0L);
                stuAcademic.setSubtotal(0L);
                stuAcademic.setStatus("0");
            }
            // new一个vo对象
            CheckAcademicVo vo = new CheckAcademicVo();
            vo.setSutId(tStudent.getStuId());
            vo.setStuName(tStudent.getStuName());
            vo.setBasicPoints(stuAcademic.getBasicPoints());
            vo.setFailedNum(stuAcademic.getFailedNum());
            vo.setSubTotal(stuAcademic.getSubtotal());
            vo.setStatus(stuAcademic.getStatus());
            // 将组装好的vo对象，加入集合
            checkAcademicVos.add(vo);
        }

        int totalPageNum = (countStu + pageSize - 1) / pageSize;


        model.addAttribute("students", checkAcademicVos);
        model.addAttribute("totalPageNum", totalPageNum);
        model.addAttribute("page", curPage);

        return "surveyTeam/stuSurvey_academicPerformance";
    }

    /**
     * 跳转测评行为表现测评
     *
     * @return
     */
    @RequestMapping("/toSurveyBehavior/{curPage}/{pageSize}")
    public String toBehavior(@PathVariable Integer curPage, @PathVariable Integer pageSize, String condition, HttpSession session, Model model) {

        // 分页查询，开始记录位置
        int begin = (curPage - 1) * pageSize;

        // 登入的测评小组成员信息
        TStudent student = (TStudent) session.getAttribute("loginSurvey");
        // 获取其所在的班级
        String classes = student.getClasses();
        // 初始化一个VoList
        List<CheckBehaviorVo> checkBehaviorVos = new ArrayList<>();
        // 查询其班级的所有同学
        List<TStudent> students = new ArrayList<>();
        int countStu = 0;
        if (null != condition) {
            students = studentMapper.findStuByClassessAndCondition(classes, condition, begin, pageSize);
            model.addAttribute("searchName", condition);
            if (students.size() >= 1) {
                countStu = students.size();
            } else {
                countStu = studentMapper.findStuCountByClasses(classes);
            }
        } else {
            students = studentMapper.findStuByClassess(classes, begin, pageSize);
            // 查询班级人数
            countStu = studentMapper.findStuCountByClasses(classes);
        }



        Prehensive prehensive = null;
        for (TStudent tStudent : students) {
            // 拿到同班同学的id
            String stuId = tStudent.getStuId();
            List<Prehensive> prehensives = prehensiveMapper.findPrehensive(stuId);
            // 判断该同学是否提交
            if (prehensives.isEmpty()) {
                // 该同学未提交行为表现
                prehensive = new Prehensive();
                prehensive.setStatus(0);
                prehensive.setPrehensivePerformanceEvaluationContent("");
                prehensive.setPoint(0L);
            } else {
                prehensive = prehensives.get(0);
            }
            // new一个vo对象
            CheckBehaviorVo vo = new CheckBehaviorVo();
            vo.setStuId(tStudent.getStuId());
            vo.setStuName(tStudent.getStuName());
            vo.setStatus(prehensive.getStatus());
            // 将组装好的vo对象，加入集合
            checkBehaviorVos.add(vo);
        }

        // 计算总页数
        int totalPageNum = (countStu + pageSize - 1) / pageSize;


        // 将vo对象放入请求域
        model.addAttribute("behaviors", checkBehaviorVos);
        // 总页数
        model.addAttribute("totalPageNum", totalPageNum);
        // 当前前页
        model.addAttribute("page", curPage);

        return "surveyTeam/stuSurvey_behaviorPerformance";
    }


    /**
     * 通过学生id获取其行为表现信息
     * @param stuId
     * @param stuName
     * @return
     */
    @PostMapping("/findBehaviorByStuId")
    @ResponseBody
    public BehaviorInfoVo findBehaviorByStuId(String stuId,String stuName) {
        BehaviorInfoVo behaviorInfoVo = new BehaviorInfoVo();
        behaviorInfoVo.setStuId(stuId);
        behaviorInfoVo.setStuName(stuName);
        // 查询所有的扣分记录
        List<Prehensive> prehensive = prehensiveMapper.findPrehensive(stuId);
        behaviorInfoVo.setPrehensive(prehensive);
        return behaviorInfoVo;
    }


    /**
     * 跳转测评小组端首页
     *
     * @return
     */
    @RequestMapping("/toSurveyIndex")
    public String toSurveyIndex() {
        return "surveyTeam/stuSurvey_index";
    }

    /**
     * 跳转测评小组个人信息页面
     *
     * @return
     */
    @RequestMapping("/toSurveyInfo")
    public String toInfo() {
        return "surveyTeam/stuSurvey_info";
    }


    /**
     * 学业表现审核，修改状态及返回驳回信息
     * @param stuId
     * @param status
     * @param msg
     * @return
     */
    @PostMapping("/changeAcademicStatus")
    @ResponseBody
    public String changeAcademicStatus(String stuId, String status, String msg) {
        int effect = academicMapper.changeStatus(stuId, status, msg);
        if (effect == 1) {
            return "ok";
        }
        return "error";
    }

    /**
     * 行为表现审核，修改状态及返回驳回信息
     * @param stuId
     * @param
     * @return
     */
    @PostMapping("/changeBehaviorStatus")
    @ResponseBody
    public String changeBehaviorStatus(String stuId, String status, String msg) {
        int effect = prehensiveMapper.changeStatus(stuId, status, msg);
        if (effect > 0) {
            return "ok";
        }
        return "error";
    }


    /**
     * 专业技能审核，修改状态和放回驳回信息
     * @param stuId
     * @param status
     * @param msg
     * @return
     */
    @PostMapping("/changeProfessionalStatus")
    @ResponseBody
    public String changeProfessionalStatus(String stuId, String status, String msg) {
        int effect = profrssionalSkillsMapper.changeStatus(stuId, status, msg);
        if (effect > 0) {
            return "ok";
        }
        return "error";
    }


    /**
     * 通过学生id获取其专业技能信息
     * @param
     * @param
     * @return
     */
    @PostMapping("/findProfessionalInfoByStuId")
    @ResponseBody
    public ProfessionalInfoVo findProfessionalInfoByStuId(String stuId, String stuName) {
        ProfessionalInfoVo professionalInfoVo = new ProfessionalInfoVo();
        professionalInfoVo.setStuId(stuId);
        professionalInfoVo.setStuName(stuName);
        // 查询所有的专业加分记录记录
        List<ProfrssionalSkills> profrssionalSkills = profrssionalSkillsMapper.findProfrssionalById(stuId);
        // 准备一个itemVoList
        List<ProfessionalItemVo> itemList = new ArrayList<>();
        for (ProfrssionalSkills profrssionalSkill : profrssionalSkills) {
            Integer itemId = profrssionalSkill.getItemId();
            // 根据itemId查询对应的条目
            TExtraPoint item= extraPointsMapper.findItemByItemId(itemId);
            // 准备一个itemVo
            ProfessionalItemVo itemVo = new ProfessionalItemVo();
            itemVo.setItemName(item.getItemName());
            itemVo.setItemScore(item.getItemScore());
            itemVo.setImgSrc(profrssionalSkill.getItemImg());
            itemList.add(itemVo);
        }
        professionalInfoVo.setProfessionalItemVos(itemList);

        return professionalInfoVo;
    }


    @GetMapping("/getBehaviorStatusNum")
    @ResponseBody
    public  List<Map<String,Object>>  getBehaviorStatusNum(HttpSession session){
        TStudent adminStu = (TStudent) session.getAttribute("loginSurvey");
        String classes = adminStu.getClasses();
        List<TStudent> students = studentMapper.findAllStuByClassess(classes);
        int unSub = 0;
        int needCheck = 0;
        int alreadyCheck = 0;
        int needEdit = 0;
        for (TStudent student : students) {
            String stuId = student.getStuId();
            List<Prehensive> prehensive = prehensiveMapper.findPrehensive(stuId);
            if(prehensive.size() == 0){
                unSub++;
                continue;
            }
            int statusCode = prehensive.get(0).getStatus();
            switch (statusCode){
                case 1:
                    // 待审核
                    needCheck ++;
                    break;
                case 2:
                    // 已审核
                    alreadyCheck++;
                    break;
                case 3:
                    // 待修改
                    needEdit++;
                    break;
            }
        }
        List<Map<String,Object>> dataList = new ArrayList<>();
        Map<String,Object> item1 = new HashMap<>();
        item1.put("value",unSub);
        item1.put("name","未提交");
        Map<String,Object> item2 = new HashMap<>();
        item2.put("value",needCheck);
        item2.put("name","待审核");
        Map<String,Object> item3 = new HashMap<>();
        item3.put("value",alreadyCheck);
        item3.put("name","已审核");
        Map<String,Object> item4 = new HashMap<>();
        item4.put("value",needEdit);
        item4.put("name","待修改");

        dataList.add(item1);
        dataList.add(item2);
        dataList.add(item3);
        dataList.add(item4);


        return dataList;
    }



    @RequestMapping("/getAcademicChartData")
    @ResponseBody
    public Map<String,Object> getAcademicChartData(HttpSession session){
        TStudent adminStu = (TStudent) session.getAttribute("loginSurvey");
        String classes = adminStu.getClasses();

        Map<String,Object> reMap = new HashMap<String, Object>();
        //查找班级的所有学生 sel:select count(*) from tb_student where classes=#{classes}
        int all = studentMapper.findCountByClass(classes);
        //查找未提交人数学业表现人数 sql:select count(*) from tb_academic  where status = #{status}  stu_id in (select stu_id from tb_student where classes=#{classes})

        int status_1 = studentMapper.findAcademicCountByStuIdAndStatus(classes,"1");
        int status_2 = studentMapper.findAcademicCountByStuIdAndStatus(classes,"2");
        int status_3 = studentMapper.findAcademicCountByStuIdAndStatus(classes,"3");

        int status_0 = all - status_1 - status_2 - status_3;

        reMap.put("status_0",status_0);
        reMap.put("status_1",status_1);
        reMap.put("status_2",status_2);
        reMap.put("status_3",status_3);

        return reMap;

    }

    @RequestMapping("/getProfrssionalSkillChartData")
    @ResponseBody
    public Map<String,Object> getProfrssionalSkillChartData(HttpSession session){

        TStudent adminStu = (TStudent) session.getAttribute("loginSurvey");
        String classes = adminStu.getClasses();

        Map<String,Object> reMap = new HashMap<String, Object>();
        //查找班级的所有学生 sel:select count(*) from tb_student where classes=#{classes}
        int all = studentMapper.findCountByClass(classes);
        //查找各职业技能提交状态人数 sql:select count(*) from tb_profrssional_skills  where status = #{status}  stu_id in (select stu_id from tb_student where classes=#{classes})

        int status_1 = studentMapper.findProfrssionalSkillCountByStuIdAndStatus(classes,"1");
        int status_2 = studentMapper.findProfrssionalSkillCountByStuIdAndStatus(classes,"2");
        int status_3 = studentMapper.findProfrssionalSkillCountByStuIdAndStatus(classes,"3");

        int status_0 = all - status_1 - status_2 - status_3;

        reMap.put("status_0",status_0);
        reMap.put("status_1",status_1);
        reMap.put("status_2",status_2);
        reMap.put("status_3",status_3);

        return reMap;
    }
}
